//
//  NetRequestClass.m
//  MyAFNetWorking
//
//  Created by jianglinfeng on 16/1/27.
//  Copyright © 2016年 jianglinfeng. All rights reserved.
//

#import "NetRequestClass.h"
#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"

@implementation NetRequestClass


//检测网络的可连接性
+ (void)NetWorkReachabilityWithURLString:(NSString *)url parameter:(NSMutableDictionary *)parameter result:(NetValueBlock)block {
    
    __block BOOL netState = NO;
    NSURL *baseURL = [NSURL URLWithString:url];
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
    NSOperationQueue *operationQueue = manager.operationQueue;
    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
                [operationQueue setSuspended:NO];
                netState = YES;
                break;
            case AFNetworkReachabilityStatusNotReachable:
                netState = NO;
            default:
                [operationQueue setSuspended:YES];
                break;
        }
        block(@(netState),parameter);
    }];
    [manager.reachabilityManager startMonitoring];
}

/************************************
 在这里判断如果有dic里有errorCode
 调用errorBlock(dic)
 没有errorCode则调用block(dic)
 **********************************/

#pragma mark GET方式请求
+ (void)NetRequestGETWithRequestURL:(NSString *)requestURLString WithParameter:(NSDictionary *)parameter WithReturnValueBlock:(ReturnValueBlock)block WithErrorCodeBlock:(ErrorCodeBlock)errorBlock WithFailureBlock:(FailuresBlock)failureBlock {

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    AFHTTPRequestOperation *option =  [manager GET:requestURLString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        block(dic);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failureBlock();
    }];
    option.responseSerializer = [AFHTTPResponseSerializer serializer];
    [option start];
}

+ (void)NetRequestPOSTWithRequestURL:(NSString *)requestURLString WithParameter:(NSDictionary *)parameter WithReturnValueBlock:(ReturnValueBlock)block WithErrorCodeBlock:(ErrorCodeBlock)errorBlock {

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 10.f;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    AFHTTPRequestOperation *option = [manager POST:requestURLString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        block(operation.responseString);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        errorBlock(@"网络连接失败！");
    }];
    option.responseSerializer = [AFHTTPResponseSerializer serializer];
    [option start];
}

#pragma mark 通用网络请求
+ (void)NetCommonRequest:(NSMutableDictionary *)parameter withUrl:(NSString *)url WithReturnValueBlock:(ReturnValueBlock)block WithErrorCodeBlock:(ErrorCodeBlock)errorBlock {

    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:parameter];
    
    [NetRequestClass NetRequestPOSTWithRequestURL:url WithParameter:dic WithReturnValueBlock:^(id returnValue) {
        
        block(returnValue);
    } WithErrorCodeBlock:^(id errorCode) {
        
        errorBlock(errorCode);
    }];
}

#pragma mark -- 上传图片
+ (void)NetUploadFilesPOSTWithRequestURL:(NSString *)requestURLString
                           withParameter:(NSMutableDictionary *)parameter
                                    data:(NSData *)imageData
                            compressData:(NSData *)compressData//压缩图片
                                    name:(NSString *)name
                                fileName:(NSString *)fileName
                                mimeType:(NSString *)mimeType
                    WithReturnValeuBlock: (ReturnValueBlock) block
                      WithErrorCodeBlock: (ErrorCodeBlock) errorBlock
                        WithFailureBlock: (FailuresBlock) failureBlock {
    
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];
    
    //设置超时时间
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 10.f;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    AFHTTPRequestOperation *op = [manager POST:requestURLString parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:name fileName:fileName mimeType:mimeType];
        [formData appendPartWithFileData:compressData name:@"compressImage" fileName:fileName mimeType:mimeType];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(operation.responseString);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",[error localizedDescription]);
        failureBlock(error);
    }];
    
    op.responseSerializer = [AFHTTPResponseSerializer serializer];
    [op start];
}

@end



























